﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace EnterpriseFramework.DataAccess.Validation
{
    /// <summary>
    /// Used to validate tthe roles.
    /// </summary>
    public class Validate<T>
    {
        /// <summary>
        /// Check if field contain value or not.
        /// </summary>
        /// <param name="FieldValue"></param>
        /// <returns></returns>
        public static bool IsRequired(T FieldValue)
        {
            if (FieldValue == null || string.IsNullOrEmpty(Convert.ToString(FieldValue)))
                return false;
            return true;
        }

        /// <summary>
        /// Check Max length of field value "can check string ,int, double,flote,..."
        /// </summary>
        /// <param name="FieldValue"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool MaxLength(T FieldValue, int maxLength)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(FieldValue)) && (FieldValue is Int16 || FieldValue is Int32 ||
                FieldValue is Int64 || FieldValue is Decimal || FieldValue is Single || FieldValue is Double ||
                FieldValue is long || FieldValue is float) &&
                string.Format("{0:0}", FieldValue).Length <= maxLength)
                return true;
            else if (Convert.ToString(FieldValue).Length <= maxLength)
                return true;
            return false;
        }

        /// <summary>
        /// Check Min length of field value "can check string ,int, double,flote,..."
        /// </summary>
        /// <param name="FieldValue"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool MinLength(T FieldValue, int minLength)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(FieldValue)) && (FieldValue is Int16 || FieldValue is Int32 ||
                FieldValue is Int64 || FieldValue is Decimal || FieldValue is Single || FieldValue is Double ||
                FieldValue is long || FieldValue is float) &&
                string.Format("{0:0}", FieldValue).Length > minLength)
                return true;
            else if (Convert.ToString(FieldValue).Length > minLength)
                return true;
            return false;
        }

        /// <summary>
        /// Check if field is positive
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static bool IsPositive(T number)
        {
            if (string.IsNullOrEmpty(Convert.ToString(number)))
                return false;
            else if (Convert.ToDecimal(number) >= 0)
                return true;

            return false;
        }


        /// <summary>
        /// Validate if field is email formate.
        /// </summary>
        /// <param name="FieldValue"></param>
        /// <returns></returns>
        public static bool IsEmail(string FieldValue)
        {
            Regex reStrict = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            if (reStrict.IsMatch(FieldValue))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Check field depend on Regular Expression.
        /// </summary>
        /// <param name="FieldValue"></param>
        /// <param name="validationExpression"></param>
        /// <returns></returns>
        public static bool IsValid(string FieldValue, string validationExpression)
        {
            Regex reStrict = new Regex(validationExpression);
            if (reStrict.IsMatch(FieldValue))
                return true;
            else
                return false;
        }
        
    }
}
